package com.gitee.zhziyun.cnfsystem.works;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gitee.zhziyun.cnfsystem.master.AppMaster;
import com.gitee.zhziyun.cnfsystem.model.Command;
import com.gitee.zhziyun.cnfsystem.model.FileAccept;
import com.gitee.zhziyun.cnfsystem.model.FileSource;
import com.gitee.zhziyun.cnfsystem.utils.JSONUtil;
import com.gitee.zhziyun.cnfsystem.utils.udputil.UDPReceiver;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class MessageWorker implements Worker {
    private final AppMaster appMaster;
    private final Queue<String> messageQueue;
    private final UDPReceiver messageReceiver;
    private final MessageHandler messageHandler;

    public MessageWorker(AppMaster appMaster) {
        this.appMaster = appMaster;
        this.messageQueue = new ConcurrentLinkedQueue<>();
        this.messageReceiver = new UDPReceiver(messageQueue, appMaster.getUserInfo().getReceivePort());
        this.messageHandler = new MessageHandler();
    }

    @Override
    public void start() {
        messageHandler.start();
        messageReceiver.start();
    }

    @Override
    public void stop() {
        messageReceiver.close();
        messageHandler.close();
    }

    private class MessageHandler extends Thread {
        private boolean running = true;

        @Override
        public void run() {
            while (running) {
                String message = messageQueue.poll();
                if (message != null) {
                    String[] parts = message.split(":", 2);
                    if (parts.length < 2) continue;  // 防止格式错误

                    try {
                        String senderIp = parts[0];
                        Command command = JSONUtil.stringToObject(parts[1], Command.class);
                        String option = command.getOption();

                        // 处理消息
                        if ("message".equals(option)) {
                            System.out.println("收到消息: " + senderIp + ":" + command.getData());
                            appMaster.fireMessage(senderIp, command.getData());
                        } else if ("file".equals(option)) {
                            System.out.println("收到文件: " + senderIp + ":" + command.getData());
                            appMaster.fireFileMessage(senderIp, JSONUtil.stringToObject(command.getData(), FileSource.class));
                        } else if ("acceptFile".equals(option)) {
                            System.out.println("收到文件接收确认: " + senderIp + ":" + command.getData());
                            appMaster.fireFileAccept(senderIp, JSONUtil.stringToObject(command.getData(), FileAccept.class));
                        }
                    } catch (JsonProcessingException e) {
                        System.err.println("JSON解析错误: " + e.getMessage());
                    } catch (Exception e) {
                        System.err.println("单播处理类出现错误: " + e.getMessage());
                    }
                }
            }
        }

        public void close() {
            running = false;
        }
    }
}
